//
//  BasePickerView.m
//  Trackit
//
//  Created by yaodaobin on 2017/6/30.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "BasePickerView.h"

#define kDuration 0.3
#define kButtonWidth 70.0f

@implementation BasePickerView

- (id)init {
    self = [super initWithFrame:CGRectMake(0.0f, 0.0f, kScreenWidth, 260)];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kScreenWidth, 44.0f)];
        headerView.backgroundColor = COLOR_LIGHT_GRAY;
        [self addSubview:headerView];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kButtonWidth, 0.0f, kScreenWidth - 2 * kButtonWidth, 44.0f)];
        self.titleLabel.backgroundColor = COLOR_LIGHT_GRAY;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        self.titleLabel.textColor = COLOR_NORMAL_SUB_INFOR;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:self.titleLabel];
        
        _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kButtonWidth, 44.0f)];
        [_leftButton setTitle:@"取消" forState:UIControlStateNormal];
        _leftButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        [_leftButton setTitleColor:[UIColor colorWithHex:0x222222 alpha:1.0] forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        [_leftButton addTarget:self action:@selector(cancle:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:_leftButton];
        
        _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - kButtonWidth, 0.0f, kButtonWidth, 44.0f)];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
        _rightButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [_rightButton setTitleColor:COLOR_MAIN forState:UIControlStateNormal];
        [_rightButton setTitle:@"完成" forState:UIControlStateNormal];
        [_rightButton addTarget:self action:@selector(ok:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:_rightButton];
        
        _backgroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        _backgroundView.backgroundColor = [UIColor blackColor];
        _backgroundView.alpha = 0.4f;
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancle:)];
        [_backgroundView addGestureRecognizer:tapGestureRecognizer];
    }
    
    return self;
}

- (void)showInView:(UIView *) view {
    if (self.superview != nil) {
        return;
    }
    UIView *window = [[UIApplication sharedApplication] keyWindow];
    
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromTop;
    [self setAlpha:1.0f];
    [self.layer addAnimation:animation forKey:@"DDLocateView"];
    
    self.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
    [window addSubview:_backgroundView];
    [window addSubview:self];
}

- (void)dismiss {
    if (_dismissBlock) {
        _dismissBlock();
    }
    CATransition *animation = [CATransition  animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.type = kCATransitionPush;
    animation.subtype = kCATransitionFromBottom;
    [self setAlpha:0.0f];
    [self.layer addAnimation:animation forKey:@"TSLocateView"];
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
    [_backgroundView performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:kDuration];
}

- (BOOL)isAppeared {
    return self.superview != nil;
}

- (void)cancle:(id)sender {
    if (_dismissBlock) {
        _dismissBlock();
    }
    [self dismiss];
}

- (void)ok:(id)sender {
    if (_dismissBlock) {
        //        _dismissBlock();
    } else {
        [self dismiss];
    }
    if(self.delegate) {
        [self.delegate actionSheet:self clickedButtonAtIndex:1];
    }
}

@end
